#include "wxsspacer.h"

#include "../wxssizer.h"

namespace {
  wxsRegisterItem<wxsSpacer> Reg( _T( "Spacer" ),_T( "Spacer" ), wxsTSpacer, _T( "布局" ), 100);
  class wxsSpacerPreview: public wxPanel {
    public:
      wxsSpacerPreview( wxWindow* Parent, const wxSize& Size ):
        wxPanel( Parent, -1, wxDefaultPosition, Size ) {}
    private:
      void OnPaint( M_Code_unused wxPaintEvent& event ) {
        wxPaintDC DC( this );
        DC.SetBrush( wxBrush( wxColour( 0, 0, 0 ), wxCROSSDIAG_HATCH ) );
        DC.SetPen( wxPen( wxColour( 0, 0, 0 ), 1 ) );
        DC.DrawRectangle( 0, 0, GetSize().GetWidth(), GetSize().GetHeight() );
      }

      DECLARE_EVENT_TABLE()
  };

  BEGIN_EVENT_TABLE( wxsSpacerPreview, wxPanel )
    EVT_PAINT( wxsSpacerPreview::OnPaint )
  END_EVENT_TABLE()

}

wxsSpacer::wxsSpacer( wxsItemResData* Data ): wxsItem( Data, &Reg.Info, flSize, 0, 0 ) {}

void wxsSpacer::OnEnumItemProperties( M_Code_unused long Flags ) {}

wxObject* wxsSpacer::OnBuildPreview( wxWindow* Parent, long Flags ) {
  if( Flags & pfExact ) {
    wxSize Sz = GetBaseProps()->m_Size.GetSize( Parent );
    return new wxSizerItem( Sz.GetWidth(), Sz.GetHeight(), 0, 0, 0, 0 );
  }
  return new wxsSpacerPreview( Parent, GetBaseProps()->m_Size.GetSize( Parent ) );
}

void wxsSpacer::OnBuildCreatingCode() {
  int Index = GetParent()->GetChildIndex( this );
  wxsSizerExtra* Extra = ( wxsSizerExtra* ) GetParent()->GetChildExtra( Index );
  if( Extra == 0 ) {
    return;
  }
  switch( GetLanguage() ) {
  case wxsCPP: {
    wxsSizeData& Size = GetBaseProps()->m_Size;
    if( Size.DialogUnits ) {
      wxString SizeName = GetCoderContext()->GetUniqueName( _T( "__SpacerSize" ) );
      Codef( _T( "wxSize %s = %z;\n" )
             _T( "%MAdd(%s.GetWidth(),%s.GetHeight(),%s);\n" ),
             SizeName.wx_str(),
             &Size,
             SizeName.wx_str(),
             SizeName.wx_str(),
             Extra->AllParamsCode( GetCoderContext() ).wx_str() );
    } else {
      Codef( _T( "%MAdd(%d,%d,%s);\n" ),
             ( int )Size.X,
             ( int )Size.Y,
             Extra->AllParamsCode( GetCoderContext() ).wx_str() );
    }
    break;
  }
  case wxsUnknownLanguage:
  default: {
    wxsCodeMarks::Unknown( _T( "wxsSpacer::OnBuildCreatingCode" ), GetLanguage() );
  }
  }
}
